package com.doophan.duuchin.util;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 统一返回结果封装
 * @author Vincent
 * @param <T> 数据体
 * @date 2020/11/15
 */
@Getter
@Setter
@Builder
public class R<T, P> implements Serializable {
    // 返回码
    private Integer code;
    // 返回信息
    private String msg;
    // 详情信息
    private T info;
    // 列表信息
    private P page;

    private R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(Integer code, String msg, T info, P page) {
        this.code = code;
        this.msg = msg;
        this.info = info;
        this.page = page;
    }

    public static R success() {
        return new R(Constant.CODE_SUCCESS, Constant.MSG_SUCCESS_DEFAULT);
    }

    public static<T> R successInfo(T data) {
        return new R(Constant.CODE_SUCCESS, Constant.MSG_SUCCESS_DEFAULT, data, null);
    }

    public static<T> R successPage(T data) {
        return new R(Constant.CODE_SUCCESS, Constant.MSG_SUCCESS_DEFAULT, null, data);
    }

    public static R fail() {
        return new R(Constant.CODE_FAIL, Constant.MSG_FAIL_DEFAULT);
    }

    public static R fail(String msg) {
        return new R(Constant.CODE_FAIL, msg);
    }
}
